﻿using System.Drawing;
using System.Threading.Tasks;
using Util.Images;
using Xunit;
using Xunit.Abstractions;

namespace Util.QrCode.Tests;

/// <summary>
/// ZXing二维码服务
/// </summary>
public class ZXingQrCodeServiceTest {
    /// <summary>
    /// 二维码服务
    /// </summary>
    private readonly IQrCodeService _service;
    /// <summary>
    /// 测试输出
    /// </summary>
    private readonly ITestOutputHelper _output;
    /// <summary>
    /// 内容
    /// </summary>
    private readonly string _content;

    /// <summary>
    /// 测试初始化
    /// </summary>
    public ZXingQrCodeServiceTest( IQrCodeService service, ITestOutputHelper output ) {
        _service = service;
        _output = output;
        _content = "https://gitee.com/util-core/util";
    }

    /// <summary>
    /// 测试获取二维码流
    /// </summary>
    [Fact]
    public void TestToStream() {
        var result = _service.Content( _content ).ToStream();
        Util.Helpers.File.Write( "./test/ToStream.png", result );
    }

    /// <summary>
    /// 测试获取二维码字节流
    /// </summary>
    [Fact]
    public void TestToBytes() {
        var result = _service.Content( _content ).ToBytes();
        Util.Helpers.File.Write( "./test/ToBytes.png", result );
    }

    /// <summary>
    /// 测试获取二维码Base64字符串
    /// </summary>
    [Fact]
    public void TestToBase64_1() {
        var result = _service.Content( _content ).ToBase64();
        string base64 = "";
        _output.WriteLine( result );
        Assert.Equal( base64, result );
    }

    /// <summary>
    /// 测试获取二维码Base64字符串 - 设置图片类型
    /// </summary>
    [Fact]
    public void TestToBase64_2() {
        var result = _service.Content( _content ).ImageType( ImageType.Jpg ).ToBase64();
        string base64 = "";
        _output.WriteLine( result );
        Assert.Equal( base64, result );
    }

    /// <summary>
    /// 测试保存二维码图片
    /// </summary>
    [Fact]
    public void TestSave() {
        _service.Content( _content ).Save( "./test/Save.png" );
    }

    /// <summary>
    /// 测试获取二维码流
    /// </summary>
    [Fact]
    public async Task TestToStreamAsync() {
        var result = await _service.Content( _content ).ToStreamAsync();
        await Util.Helpers.File.WriteAsync( "./test/ToStreamAsync.png", result );
    }

    /// <summary>
    /// 测试获取二维码字节流
    /// </summary>
    [Fact]
    public async Task TestToBytesAsync() {
        var result = await _service.Content( _content ).ToBytesAsync();
        await Util.Helpers.File.WriteAsync( "./test/ToBytesAsync.png", result );
    }

    /// <summary>
    /// 测试保存二维码图片
    /// </summary>
    [Fact]
    public async Task TestSaveAsync_1() {
        await _service.Content( _content ).SaveAsync( "./test/SaveAsync_1.png" );
    }

    /// <summary>
    /// 测试保存二维码图片 - 设置二维码尺寸
    /// </summary>
    [Fact]
    public async Task TestSaveAsync_2() {
        await _service.Content( _content ).Size( QrSize.Small ).SaveAsync( "./test/SaveAsync_Small.png" );
        await _service.Content( _content ).Size( QrSize.Middle ).SaveAsync( "./test/SaveAsync_Middle.png" );
        await _service.Content( _content ).Size( QrSize.Large ).SaveAsync( "./test/SaveAsync_Large.png" );
    }

    /// <summary>
    /// 测试保存二维码图片 - 设置前景色
    /// </summary>
    [Fact]
    public async Task TestSaveAsync_3() {
        await _service.Content( _content ).Color( Color.Red ).SaveAsync( "./test/SaveAsync_3.png" );
    }

    /// <summary>
    /// 测试保存二维码图片 - 设置背景色
    /// </summary>
    [Fact]
    public async Task TestSaveAsync_4() {
        await _service.Content( _content ).BgColor( Color.Aquamarine ).SaveAsync( "./test/SaveAsync_4.png" );
    }

    /// <summary>
    /// 测试保存二维码图片 - 设置边距
    /// </summary>
    [Fact]
    public async Task TestSaveAsync_5() {
        await _service.Content( _content ).Margin( 5 ).SaveAsync( "./test/SaveAsync_5.png" );
    }

    /// <summary>
    /// 测试保存二维码图片 - 设置图标
    /// </summary>
    [Fact]
    public async Task TestSaveAsync_6() {
        var iconPath = Util.Helpers.Common.GetPhysicalPath( "./Icons/icon.jpg" );
        await _service.Content( _content ).Icon( iconPath ).SaveAsync( "./test/SaveAsync_6.png" );
    }
}